//
//  ModifyCipherController.swift
//  YiYaoHui
//
//  Created by HM on 15/12/4.
//  Copyright © 2015年 TMPercy. All rights reserved.
//

import UIKit
import Alamofire

class ModifyCipherController: UIViewController {
    
    @IBOutlet weak var oldCipherTF: UITextField!
    
    @IBOutlet weak var newCipherTF: UITextField!
    
    @IBOutlet weak var modifyButton: UIButton!
    
    @IBOutlet weak var modifyView: UIView!
    
    @IBOutlet weak var visiableButton: UIButton!
    
    let modifyCipherURL = "http://112.74.131.194:8080/MedicineProject/App/user/updatePassword"
    
    var oldCipher:String?
    var newCipher:String?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        oldCipherTF.delegate = self
        newCipherTF.delegate = self
        modifyView.layer.borderWidth = 1.0
        modifyView.layer.borderColor = UIColor(red: 235/255, green: 235/255, blue: 235/255, alpha: 1).CGColor
        modifyButton.layer.cornerRadius = 5
    }
    
    override func viewWillAppear(animated: Bool) {
        navigationController?.navigationBarHidden = false
        //去掉箭头右边文字
        UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffset(horizontal: 0, vertical: -60), forBarMetrics: .Default)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    @IBAction func visiable(sender: AnyObject) {
        if newCipherTF.secureTextEntry == false {
            newCipherTF.secureTextEntry = true
        }
        else {
            newCipherTF.secureTextEntry = false
            newCipherTF.adjustsFontSizeToFitWidth = true
        }
    }
    
    @IBAction func confirmModify(sender: AnyObject) {
        
        //编辑完了按空白处收键盘
//        dismissKeyboard()
        
        //手机号不能为空
        if ((oldCipherTF.text != nil  && newCipherTF.text != nil) && (oldCipherTF.text != "" && newCipherTF.text != "")) {
            Alamofire.request(.POST, modifyCipherURL, parameters: ["oldPassword":oldCipherTF.text!,"newPassword":newCipherTF.text!], encoding: .URL, headers: nil).responseJSON { (_, _, result) -> Void in
                
                print(self.oldCipherTF.text,self.newCipherTF.text)
                
                //先判断返回的值是否为空，不为空就执行这一步
                if let  value = result.value {
                    //再判断返回的值是success还是fail
                    let state = value["result"] as! String
                    if state == "success" {
                        
                        let alert = UIAlertController(title: "提示", message: "修改密码成功！", preferredStyle: UIAlertControllerStyle.Alert)
                        //请求成功则按“确定后”跳到登录页面重新登录，在登录页面保存新密码
                        let action = UIAlertAction(title: "确定", style: .Default, handler: { (alertAction) -> Void in
                            let loginController = self.storyboard?.instantiateViewControllerWithIdentifier("LoginController") as! LoginController
                            self.navigationController?.pushViewController(loginController, animated: true)
                            
                        })
                        alert.addAction(action)
                        self.presentViewController(alert, animated: true, completion: nil)
                        
                        
                        print(result.value)
                    } else { //如果返回fail
                    
                    }
                }
                else { //如果返回nil
                    
                }
            }
        } else {
//            self.disableButton()
        }

    }
    
}

//text field 的
extension ModifyCipherController : UITextFieldDelegate {
    //点击空白处收键盘
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        oldCipherTF.resignFirstResponder()
        newCipherTF.resignFirstResponder()
    }
    
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        oldCipherTF.resignFirstResponder()
        newCipherTF.resignFirstResponder()
        return true
    }
    
    //接收原密码、新密码的text
    func textFieldDidBeginEditing(textField: UITextField) {
         oldCipher = oldCipherTF.text
        newCipher = newCipherTF.text
    }

    
}